#!/bin/bash

echo "========================================"
echo "🔧 修复批量删除405错误"
echo "========================================"
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 1. 检查代码是否正确
echo "1. 检查前端代码..."
CODE_CHECK=$(grep "bulk_delete" /aiops/frontend/src/services/api.js)

if echo "$CODE_CHECK" | grep -q "bulk_delete"; then
    echo -e "${GREEN}✅ 前端代码正确${NC}"
    echo "   找到: bulkDeleteAuditLogs: ...bulk_delete/..."
else
    echo -e "${RED}❌ 前端代码错误${NC}"
    echo "   未找到正确的 bulk_delete URL"
    exit 1
fi

echo ""

# 2. 停止Vite
echo "2. 重启Vite开发服务器..."
pkill -f "vite --host"
sleep 2

# 3. 清理Vite缓存
echo "3. 清理Vite缓存..."
rm -rf /aiops/frontend/node_modules/.vite
echo -e "${GREEN}✅ Vite缓存已清除${NC}"

echo ""

# 4. 重启Vite
echo "4. 启动Vite..."
cd /aiops/frontend
nohup npm run dev > /tmp/vite.log 2>&1 &
VITE_PID=$!

sleep 3

# 检查Vite是否成功启动
if ps -p $VITE_PID > /dev/null; then
    echo -e "${GREEN}✅ Vite已启动 (PID: $VITE_PID)${NC}"
else
    echo -e "${RED}❌ Vite启动失败${NC}"
    echo "请查看日志: tail -f /tmp/vite.log"
    exit 1
fi

echo ""

# 5. 检查Django服务
echo "5. 检查Django服务..."
if ps aux | grep -q "[m]anage.py runserver"; then
    echo -e "${GREEN}✅ Django服务正在运行${NC}"
else
    echo -e "${YELLOW}⚠️  Django服务未运行，正在启动...${NC}"
    cd /aiops/backend
    nohup python3 manage.py runserver 0.0.0.0:8000 > /tmp/django.log 2>&1 &
    sleep 3
    if ps aux | grep -q "[m]anage.py runserver"; then
        echo -e "${GREEN}✅ Django服务已启动${NC}"
    else
        echo -e "${RED}❌ Django服务启动失败${NC}"
        exit 1
    fi
fi

echo ""

# 6. 测试API
echo "6. 测试批量删除API..."
sleep 2

HTTP_CODE=$(curl -X POST "http://localhost:8000/api/audit-logs/bulk_delete/" \
  -H "Content-Type: application/json" \
  -d '{"ids": []}' \
  -w "%{http_code}" \
  -s -o /dev/null)

if [ "$HTTP_CODE" = "401" ]; then
    echo -e "${GREEN}✅ API测试通过！${NC}"
    echo "   状态码: 401 (需要认证 - URL正确)"
elif [ "$HTTP_CODE" = "400" ]; then
    echo -e "${GREEN}✅ API测试通过！${NC}"
    echo "   状态码: 400 (参数错误 - URL正确)"
elif [ "$HTTP_CODE" = "405" ]; then
    echo -e "${RED}❌ API测试失败！${NC}"
    echo "   状态码: 405 (方法不允许 - URL错误)"
    echo ""
    echo "   请手动检查:"
    echo "   1. Django服务是否正确启动"
    echo "   2. 后端代码是否有语法错误"
else
    echo -e "${YELLOW}⚠️  状态码: $HTTP_CODE${NC}"
fi

echo ""
echo "========================================"
echo "🎯 现在请执行以下操作："
echo "========================================"
echo ""
echo "1. ${YELLOW}完全关闭浏览器${NC}"
echo ""
echo "2. ${YELLOW}重新打开浏览器${NC}"
echo ""
echo "3. ${YELLOW}访问系统并登录${NC}"
echo "   http://localhost:8080"
echo ""
echo "4. ${YELLOW}进入审计日志页面${NC}"
echo "   http://localhost:8080/#/audit-log"
echo ""
echo "5. ${YELLOW}按 Ctrl+Shift+R 强制刷新${NC}"
echo ""
echo "6. ${YELLOW}测试批量删除功能${NC}"
echo "   - 选择几条日志"
echo "   - 点击 '批量删除' 按钮"
echo "   - 应该成功删除"
echo ""
echo "========================================"
echo ""
echo "💡 ${GREEN}如果还是405错误，请:${NC}"
echo ""
echo "1. 打开浏览器开发者工具 (F12)"
echo "2. 切换到 Network 标签"
echo "3. 勾选 'Disable cache'"
echo "4. 再次按 Ctrl+Shift+R"
echo ""
echo "或者使用隐私/无痕模式:"
echo "  Ctrl+Shift+N (Chrome/Edge)"
echo "  Ctrl+Shift+P (Firefox)"
echo ""
echo "========================================"
